import Vue from 'vue'
import Vuex from 'vuex'
import $cache from '@/common/cache.js';
import {
	filePath,
	getCityRegion
} from "@/apis/index";
const TOKEN = $cache.getStorageSync("token");
const USER_INFO = $cache.getStorageSync("userInfo");
const LATESTMOILE = uni.getStorageSync("latestMoile");
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		// 前端token
		token: TOKEN,
		// 用户信息 头像 昵称
		userInfo: USER_INFO,
		//最近登录手机号
		latestMoile: LATESTMOILE,
		imagePath: "",
		polygons: [], //行政区划
		isUserPhone:false,//首次进入判断微信手机号是否与登录手机号一致
	},
	getters: {
		// 用户是否登录
		hasLogin: state => {
			let _token = state.token;
			if (_token == undefined) {
				_token = $cache.getStorageSync("token");
			}
			if (_token) {
				return true;
			} else {
				return false
			}
		},
		// 用户是否有手机号
		hasMobile: state => {
			let _mobile = state.userInfo.mobile;
			if (_mobile) {
				return true;
			} else {
				return false
			}
		},
		tabBar: state => {
			// let canViewPunch = state.userInfo.roleCode;
				return [{
						name: 'home',
						activeName: 'home',
						pagePath: '/pages/threeTerminal/index',
						text: '首页',
						iconPath: '/static/imgs/icon1.png',
						selectedIconPath: '/static/imgs/icon1.png'
					},
					{
						name: 'feedback',
						activeName: 'feedback',
						pagePath: '/pages/threeTerminal/taskTabba',
						text: '任务',
						iconPath: '/static/imgs/icon1.png',
						selectedIconPath: '/static/imgs/icon1.png'
					},
					{
						name: 'people',
						activeName: 'people',
						pagePath: 'pages/user/user',
						text: '我的',
						iconPath: '/static/imgs/icon1.png',
						selectedIconPath: '/static/imgs/icon1.png'
					}
				]
			
		}
	},
	mutations: { 
		SET_USERPHONE(state, isUserPhone) {
			state.isUserPhone = isUserPhone; 
		},
		SET_TOKEN(state, token) {
			state.token = token;
			$cache.setStorageSync("token", token);
		},
		SET_OPENID(state, openid) {
			$cache.setStorageSync("openid", openid);
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo;
			$cache.setStorageSync("userInfo", userInfo);
		},
		SET_LOGOUT(state) {
			state.token = "";
			state.userInfo = "";
			$cache.removeStorageSync("userInfo");
			$cache.removeStorageSync("token");
		},
		SET_LOGINMOILE(state, latestMoile) {
			state.latestMoile = latestMoile;
			uni.setStorageSync("latestMoile", latestMoile);
		},
		SET_IMAGEPATH(state, imagePath) {
			state.imagePath = imagePath;
		},
		SET_POLYGONS(state, points) {
			let pointsArr = [];
			points.forEach((t, i) => {
				if (i % 2 == 0) {
					pointsArr.push({
						longitude: t,
						latitude: points[i + 1]
					})
				}
			})
			console.log(pointsArr, "pointsArrpointsArr")
			state.polygons.push({
				points: pointsArr, //经纬度数组 
				strokeWidth: 2, //描边的宽度
				strokeColor: "#4498EC", //描边的颜色
				fillColor: "#4498EC40" //填充颜色
			});
		},
	},
	actions: {
		getFilePath({
			commit
		}) {
			return new Promise((resolve, reject) => {
				0&&filePath().then(result => {
					let _data = result;
					if (_data.code === 0) {
						commit("SET_IMAGEPATH", _data.msg);
						resolve(_data.result);
					} else {
						reject(_data.msg);
					}
				}).catch(err => {
					console.error("err: ", err);
					reject(err);
				});
			})
		},
		getCityRegionList({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getCityRegion({
					keyword: 360981,
					get_polygon: 2
				}).then(result => {
					console.log(result, "resultresult")
					let _data = result;
					if (_data.status === 0) {
						commit("SET_POLYGONS", _data.result[0][0].polygon[0]);
						resolve(_data.result);
					} else {
						reject(_data.message);
					}
				}).catch(err => {
					console.error("err: ", err);
					reject(err);
				});
			})
		},
	}
})
export default store